package br.ufc.cloud.service.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.microsoft.windowsazure.services.blob.client.CloudBlobClient;
import com.microsoft.windowsazure.services.blob.client.CloudBlobContainer;
import com.microsoft.windowsazure.services.blob.client.CloudBlockBlob;
import com.microsoft.windowsazure.services.blob.client.ListBlobItem;
import com.microsoft.windowsazure.services.core.storage.CloudStorageAccount;
import com.microsoft.windowsazure.services.core.storage.StorageException;

public class NoteDAO {

	public static final String storageConnectionString = "DefaultEndpointsProtocol=http;"
			+ "AccountName=qnotes;"
			+ "AccountKey=OP/PQg4Ag9DT2JV+Q/EdmvCnSf0Yvucp0qa36t+rmT6LHGuq/2kGoSbDlsu4OVHBx5i/XK+wfZyc37NrhJl4ZA==";

	CloudStorageAccount storageAccount;
	CloudBlobClient blobClient;
	CloudBlobContainer container;
	ListBlobItem blockItem;

	public void setBlob() throws InvalidKeyException, URISyntaxException,StorageException {

		storageAccount = CloudStorageAccount.parse(storageConnectionString);
		blobClient = storageAccount.createCloudBlobClient();
		container = blobClient.getContainerReference("images");
		container.createIfNotExist();

	}

	public String getUriFromBlob(String nameImg){
		

		try {
				setBlob();
			
			blockItem = container.getBlockBlobReference(nameImg);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (StorageException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return (blockItem.getUri()).toString();

	}

	public void uploadImage(String pathImg, String nameImg)throws FileNotFoundException, StorageException, IOException,URISyntaxException, InvalidKeyException {

		String path = pathImg;
		String name = nameImg;

		setBlob();
		CloudBlockBlob block = container.getBlockBlobReference(name);
		File source = new File(path);
		block.upload(new FileInputStream(source), source.length());
	}

	public void createNote(String note, String title, String pathSQLite, String pathBlob, int id)
			throws SQLException {

		ConnectionManagerMSJDBC.setDriver();

		String query = "INSERT INTO Note (text, pathImg, pathSQL, title, id) values (?,?,?,?, ?)";
		PreparedStatement ps = ConnectionManagerMSJDBC.PrepStatement(query);
		ps.setString(1, note);
		ps.setString(2, pathBlob);
		ps.setString(3, pathSQLite);
		ps.setString(4, title);
		ps.setInt(5, id);
		ps.execute();
	}
	
	public ArrayList<NoteBean> getNotes() throws SQLException{
		
		ConnectionManagerMSJDBC.setDriver();
		ArrayList<NoteBean> nb = new ArrayList<NoteBean>();
		String query = "SELECT title, text FROM note";
		//PreparedStatement ps = ConnectionManagerMSJDBC.PrepStatement(query);
		ResultSet rs;
		
//			rs = ps.executeQuery(query);
		rs = ConnectionManagerMSJDBC.getStatement().executeQuery(query);
		
		try {
			while (rs.next()){
				NoteBean note = new NoteBean();
				//note.setPathBlob(rs.getString("pathImg"));
				note.setText(rs.getString("text"));
				note.setTitle(rs.getString("title"));
				nb.add(note);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return nb;
}

	public void deleteNote(int id) throws SQLException {
		ConnectionManagerMSJDBC.setDriver();
		int id_note = id;
		String query = "DELETE FROM Note Where ID = ?";
		PreparedStatement ps = ConnectionManagerMSJDBC.PrepStatement(query);
		ps.setInt(1, id_note);
		ps.execute();
	}

	public void updateNote(String texto, String PathIMG, String data,
			String hora, int id) {
		String query = "";
	}
}
